package com.mach.platform.controller;

import com.mach.platform.constants.CommonConstants;
import com.mach.platform.domain.Response;
import com.mach.platform.domain.base.Role;
import com.mach.platform.service.RoleService;
import com.mach.platform.utils.security.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author Anonymous
 * @Description:
 * @Package: com.mach.platform.com.mach.platform.controller
 * @time 2018/04/12 17:40
 */
@Api(tags = { "角色管理接口" })
@RestController
@RequestMapping("$role")
public class RoleCtrl extends BaseCtrl<Role> {

	@Autowired
	private RoleService roleService;

	/**
	 * -- @GetMapping(value = "/xxx/{id}")
	 * -- @PathVariable(value = "id", required = false) Long id
	 * 单个数据 的接口
	 *
	 * @param id 数据的id
	 * @return
	 */
	@Override
	public Response info (Long id) {
		return null;
	}

	/**
	 * 获取列表数据
	 * --@GetMapping(value = "/xxxs")
	 * -- @PageableDefault(size = 25) Pageable pageable
	 *
	 * @param pageable
	 * @param params
	 * @return
	 */
	@ApiOperation(value = "列表--分页", produces = "application/json", notes = "列表参数：size(每页数量),page(页码),searchParam(查询内容)")
	@GetMapping(value = "/list")
	@Override
	public Response list (@PageableDefault(size = 25) Pageable pageable, @RequestParam Map params) {

		Page<Role> dbResult = roleService.list(pageable, params);

		return Response.ok(dbResult);
	}

	/**
	 * -- @DeleteMapping("/xxx")
	 * -- @RequestBody Long[] ids
	 * 删除数据
	 *
	 * @param ids
	 * @return
	 */
	@ApiOperation(value = "删除", notes = "接口备注：无", produces = "application/json")
	@DeleteMapping("/del")
	@Override
	public Response delete (@RequestBody Long[] ids) {

		if (Arrays.asList(ids).stream().anyMatch(item -> item.equals(1L))) {
			return Response.error("注意，没人能干掉宇宙强者角色...");
		}

		Integer effectedNums = roleService.delete(ids);

		return Response.ok(effectedNums);
	}

	/**
	 * 保存一个 对象
	 * --@PostMapping("/xxx")
	 * --@RequestBody T t
	 *
	 * @param role
	 * @return
	 */
	@ApiOperation(value = "新增", produces = "application/json")
	@PostMapping("/save")
	@Override
	public Response save (@RequestBody Role role) {

		Role dbResult = roleService.save(role);

		return Response.ok(dbResult);
	}

	/**
	 * 更新一个对象
	 * --@PutMapping("/xxx")
	 * --@RequestBody T t
	 *
	 * @param role
	 * @return
	 */
	@ApiOperation(value = "修改保存", produces = "application/json")
	@PutMapping("/upd")
	@Override
	public Response update (@RequestBody Role role) {
		//非超级管理员账号不能修改超级管理员账号内容
		if (role.getId().equals(CommonConstants.ADMIN_ID) && !SecurityUtils.getCurrentUser().getId().equals(CommonConstants.ADMIN_ID)) {
			return Response.error("您不能修改这个牛逼的角色。");
		}

		Role dbResult = roleService.update(role);

		return Response.ok(dbResult);
	}


	/**
	 * 更新 角色对应的用户数据
	 * @param roleId
	 * @param userIds
	 * @return
	 */
	@PostMapping("updateUsers/{id}")
	public Response updateRoleUsersRelations (@PathVariable("id") Long roleId, @RequestBody List<Long> userIds) {

		int effes = roleService.insertUserRealtion(roleId, userIds);

		return Response.ok(effes);
	}

	/**
	 * 更新 角色对应的菜单数据
	 * @param roleId
	 * @param menuIds
	 * @return
	 */
	@PostMapping("updateMenus/{id}")
	public Response updateRoleMenusRelations (@PathVariable("id") Long roleId, @RequestBody List<Long> menuIds) {
		if (menuIds.contains(1L)) {
			menuIds.remove(1L);
		}
		if (menuIds.contains(2L)) {
			menuIds.remove(2L);
		}

		int effes = roleService.insertMenuRealtion(roleId, menuIds);

		return Response.ok(effes);
	}

}
